<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.LocalMethods"%>
<script>
$(document).ready(function() {

$('#local').change(function(event) {
		var defevent = $("select#defevent").val();
        if(defevent == "zarejestrowane") {
        	var local_id = $('#local').val();
        $.get('../addEventResult1', {
                local_id : local_id
        },  function(response) {
            console.log(response);
            var select = $('#event');
            select.find('option').remove();
              $.each(response, function(index, value) {
                  if (value.trim() != ""){
            	  $.ajax({
           	       url: "../eventDetails",
           	       type: "GET",//type of posting the data
           	       data: {
           		       event_id: value
           	       },
           	       dataType: 'json',
           	       success: function (json) {
					var data = json;
           	    	 $('<option>').val(data.userevent_id).text(data.eventname+": "+ data.user_id + "-> " + data.start + " - " + data.end).appendTo(select);
              	    },
        	       error: function(xhr, ajaxOptions, thrownError){
        	       },
        	       timeout : 15000
        	  });
                     }
                 });
                 
           });
}
   });
   });
   </script>
<div class="page-header">
	<h2 class="page-title">Dodaj rezultat rozgrywki</h2>
</div>

<br />

<div id="content">
<form action="../addEventResult1" method="post" name="stolik"> 
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						LocalMethods l_m = new LocalMethods();
						 Integer user_id = (Integer) session.getAttribute("user_id");
							response.setContentType("text/html");
							if (user_id != null) {
						List<Local> llist = l_m.listAllLocals(sessionFactory, s);
						s.getTransaction().commit();
					%> 
<table>
<tr>
<td>
Wybierz lokal: </td>
<td>
 <select name="local" id="local" class="postform" style="width:250px">
                    <option value="0">-- Wybierz lokal --</option>
              
<%                if (llist.size() > 0) {
	for (Local l1 : llist) {
%>
                    <option value="<%=l1.getLocal_id()%>"><%=l1.getStreet() + " " + l1.getLocal_number() + " , "+ l1.getCity() %></option>
                    <%}
	}

	}%>
                      </select>
</td>
</tr>

<tr>
<td>
Rodzaj wydarzenia: </td>
<td>
 <select name="defevent" id="defevent" class="postform" style="width:250px">
                    <option value="niezarejestrowane">Wydarzenie niezarejestrowane</option>
              		<option value="zarejestrowane"> Wydarzenie zarejestrowane </option>
             </select>
</td>
</tr>
<tr>
<td>
		<select id="event" name="event">
						<option value="0" selected>Wybierz wydarzenie</option>
						</select>
</td>
<td>
</td>
</tr>
</table>
<input type="submit" class="mainBtn" value="Dalej" />
</form>
</div>